package ntd.util.frame.action.game;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.json.JSONUtil;
import ntd.action.ActionAbs;
import ntd.action.bo.ActionInfo;
import ntd.action.bo.ActionOutBo;
import ntd.util.config.common.exception.ComException;
import ntd.util.frame.data.factory.FactoryData;
import ntd.util.frame.data.tool.ToolMysql;

import java.sql.SQLException;
import java.util.List;

public class ActionGameRoleCreate extends ActionAbs {

    @Override
    public ActionInfo info() {
        return new ActionInfo("Game角色创建", "game-role-create", new ActionGameRoleCreate());
    }

    @Override
    public ActionOutBo handle() {
        
        //数据库
        ToolMysql toolMysql = new ToolMysql(FactoryData.dataSource(actionInBo.dsId()));
        Db db = toolMysql.db;

        try {
            List<Entity> entities = db.find(Entity.create("game_element").set("dict_rpg_el_type", "bff234dfa59e4135afd19a5d594b270b"));

            // 随机获得一个角色形象
            Entity roleEl = entities.get(RandomUtil.randomInt(entities.size()));

            // 属性构建
            GameRole.Property property = new GameRole.Property();
            property.setHp(RandomUtil.randomInt(50, 150));
            property.setCurrentHp(property.getHp());
            property.setMp(RandomUtil.randomInt(50, 150));
            property.setCurrentMp(property.getMp());
            property.setPhysical(RandomUtil.randomInt(5, 15));
            property.setPower(RandomUtil.randomInt(5, 15));
            property.setIntelligence(RandomUtil.randomInt(5, 15));
            property.setAgile(RandomUtil.randomInt(5, 15));
            property.setLucky(RandomUtil.randomInt(5, 15));
            property.setSavvy(RandomUtil.randomInt(5, 15));

            Entity entity = new Entity("game_role");
            entity.set("id", IdUtil.simpleUUID());
            entity.set("name", actionInBo.getParamStr("name"));
            entity.set("element_id", roleEl.get("id"));
            entity.set("property", JSONUtil.toJsonStr(property));
            entity.set("property_original", JSONUtil.toJsonStr(property));

            db.insert(entity);

            return new ActionOutBo(entity);
        } catch (SQLException e) {
            throw new ComException(e.getMessage());
        }
    }
}
